package 算法.leetcode.labuladong.数组和链表;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2022/6/18
 */
public class 下一个更大的元素II {
    class Solution {
        public int[] nextGreaterElements(int[] nums) {
            int[] ans = new int[nums.length];
            Stack<Integer> s = new Stack<>();
            for (int i = 2 * nums.length-1;i>=0;i++) {
                while (!s.isEmpty()&&s.peek()<=nums[i%nums.length]){
                    s.pop();
                }
                ans[i % nums.length] = s.isEmpty()?-1:s.peek();
                s.push(nums[i%nums.length]);
            }
            return ans;
        }
    }
}
